package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityDishesPoint;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.SaveDishesPlacesRequest;
import com.ocom.common.response.payconfig.GetPlacePlanListResponse;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.vo.payconfig.DishesPointListVo;
import com.ocom.common.vo.payconfig.DishesPointOrgVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoDishesPoint;
import com.ocom.payconfig.service.DishesPointService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class DishesPointServiceImpl extends ServiceImpl<IDaoDishesPoint, EntityDishesPoint> implements DishesPointService {

    @Autowired
    private IDaoDishesPoint iDaoDishesPoint;


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public DishesPointOrgVo selectPoint(Long comId, String pointOrg) {
        List<String> isStr = Arrays.asList(pointOrg.split(","));
        List<Integer> isInt = new ArrayList<>();
        CollectionUtils.collect(isStr, new Transformer() {
            @Override
            public Object transform(Object obj) {
                return Integer.valueOf(obj.toString());
            }
        }, isInt);
        return iDaoDishesPoint.selectPoint(comId, isInt);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public EntityDishesPoint selectLastDate(Long comId, Integer pointId, Integer mealNo, Date mealDate) {
        QueryWrapper<EntityDishesPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("meal_date");
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("point_id", pointId);
        queryWrapper.eq("meal_no", mealNo);
        queryWrapper.le("meal_date", mealDate);
        queryWrapper.orderByDesc("meal_date");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<DishesPointListVo> selectPointDishes(@Param("comId") Long comId,
                                                     @Param("restPoint") Integer restPoint,
                                                     @Param("lastDate") String lastDate,
                                                     @Param("mealNo") Integer mealNo,
                                                     @Param("pointId") Integer pointId,
                                                     @Param("thisOrg") List<Integer> thisOrg) {
        return iDaoDishesPoint.selectPointDishes(
                comId,
                restPoint,
                lastDate,
                mealNo,
                pointId,
                thisOrg
        );
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<GetPlacePlanListResponse> getPlacePlanList(@Param("comId") Long comId,
                                                           @Param("merchId") Integer merchId,
                                                           @Param("pointId") Integer pointId) {
        String start = DateUtil.getDate();

        return iDaoDishesPoint.getPlacePlanList(
                comId,
                0,
                pointId,
                start
        );
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityDishesPoint> getPointPlace(@Param("comId") Long comId,
                                                 @Param("pointId") Integer pointId,
                                                 @Param("mealDate") String mealDate,
                                                 @Param("mealNo") Integer mealNo) {
        return iDaoDishesPoint.getPointPlace(comId,
                pointId,
                mealDate,
                mealNo);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    public void savePointPlaceBatch(Long companyId, SaveDishesPlacesRequest request) {
        if(CommonUtil.isNull(request.getPlaceIds())){
            QueryWrapper<EntityDishesPoint> queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("com_id",request.getComId());
            queryWrapper2.eq("point_id",request.getPointId());
            queryWrapper2.eq("dishes_id",request.getDishesId());
            queryWrapper2.eq("price_mode",request.getPriceMode());
            queryWrapper2.eq("meal_no",request.getMealNo());
            queryWrapper2.eq("meal_date",request.getMealDate());
            this.remove(queryWrapper2);
            return;
        }

        List<String> isStr = Arrays.asList(request.getPlaceIds().split(","));
        List<Integer> placeList = new ArrayList<>();
        CollectionUtils.collect(isStr, new Transformer() {
            @Override
            public Object transform(Object obj) {
                return Integer.valueOf(obj.toString());
            }
        }, placeList);

        QueryWrapper<EntityDishesPoint> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("com_id",request.getComId());
        queryWrapper2.eq("point_id",request.getPointId());
        queryWrapper2.eq("dishes_id",request.getDishesId());
        queryWrapper2.eq("price_mode",request.getPriceMode());
        queryWrapper2.eq("meal_no",request.getMealNo());
        queryWrapper2.eq("meal_date",request.getMealDate());
        queryWrapper2.notIn("dishes_point",placeList);
        this.remove(queryWrapper2);

        for (Integer placeId: placeList) {
            //pointId、mealDate、mealNo、dishesId、priceMode匹配
            QueryWrapper<EntityDishesPoint> queryWrapper = new QueryWrapper();
            queryWrapper.eq("com_id",request.getComId());
            queryWrapper.eq("point_id",request.getPointId());
            queryWrapper.eq("dishes_id",request.getDishesId());
            queryWrapper.eq("price_mode",request.getPriceMode());
            queryWrapper.eq("meal_no",request.getMealNo());
            queryWrapper.eq("meal_date",request.getMealDate());
            queryWrapper.eq("dishes_point",placeId);
            //comId、pointId、mealDate、mealNo、dishesId、priceMode、placeId
            EntityDishesPoint entityDishesPoint1 = this.getOne(queryWrapper);
            if (entityDishesPoint1 == null) {
                EntityDishesPoint entityDishesPoint =new EntityDishesPoint();
                entityDishesPoint.setComId(request.getComId());
                entityDishesPoint.setPointId(request.getPointId());
                entityDishesPoint.setDishesId(request.getDishesId());
                entityDishesPoint.setMealDate(DateUtils.strToDate2(request.getMealDate()));
                entityDishesPoint.setMealNo(request.getMealNo());
                entityDishesPoint.setPriceMode(request.getPriceMode());
                entityDishesPoint.setDishesPoint(placeId);
                this.save(entityDishesPoint);
            }
        }
    }

}
